package xyz.floatinglife.main;

import xyz.floatinglife.service.ExcelBatchService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * 2022年1月17日10点47分
 *
 * excel批量处理的图形化界面类
 *
 * @author flashylife
 *
 */
public class ExcelBatchJFrame extends JFrame {
    /**
     * 界面宽度
     */
    private static final int WIDTH = 600;
    /**
     * 界面高度
     */
    private static final int HEIGHT = 300;

    /**
     * 标签
     */
    private JLabel jLabel1, jLabel2, jLabel3;

    /**
     * 按钮
     */
    private JButton jButton;

    /**
     * 文本框
     */
    private JTextField jTextField1, jTextField2;

    /**
     * 面板
     */
    private JPanel jPanel1, jPanel2;

    /**
     * 文本区
     */
    private JTextArea jTextArea;

    /**
     * 滚动条
     */
    private JScrollPane jsp;

    /**
     * 构造方法
     * 默认初始化
     */
    public ExcelBatchJFrame() {
        jButton = new JButton("点我开始合并");

        jLabel1 = new JLabel("文件夹路径:");
        jLabel2 = new JLabel("忽略列数:");
        jLabel3 = new JLabel("日志信息:");

        jTextField1 = new JTextField(20);
        jTextField2 = new JTextField("2",5);

        jPanel1 = new JPanel();
        jPanel2  = new JPanel();

        jTextArea = new JTextArea();

        jsp = new JScrollPane(jTextArea);

    }

    /**
     * 设置属性并事件监听
     */
    public void setPro() {
        jPanel1.setLayout(new FlowLayout(0,5,10));
        jPanel1.add(jLabel1);
        jPanel1.add(jTextField1);
        jPanel1.add(jLabel2);
        jPanel1.add(jTextField2);
        jPanel1.add(jButton);
        jPanel1.setPreferredSize(new Dimension(0, 40));
        jButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        this.jButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String path = jTextField1.getText();
                String num = jTextField2.getText();
                if (!num.matches("\\d")) {
                    JOptionPane.showMessageDialog(null, "请正确的输入忽略的列");
                    return;
                }
                ExcelBatchService instance = ExcelBatchService.getInstance();
                instance.setIgnoreCellNum(Integer.valueOf(num));
                String info = instance.handleExcel(path, jTextArea);
                if ("success".equals(info)) {
                    JOptionPane.showMessageDialog(null, info);
                } else {
                    JOptionPane.showMessageDialog(null, info, "警告", JOptionPane.WARNING_MESSAGE);
                }
            }
        });

        jLabel3.setPreferredSize(new Dimension(0,35));
        jPanel2.setLayout(new BorderLayout());
        jPanel2.add(jLabel3, BorderLayout.NORTH);
        jPanel2.add(jsp, BorderLayout.CENTER);

        add(jPanel1, BorderLayout.NORTH);
        add(jPanel2);
    }

    /**
     * 启动方法
     */
    public void start() {
        this.setPro();
        this.setTitle("excelbatch by flashylife");
        this.setSize(WIDTH, HEIGHT);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.setResizable(false);
    }

    /**
     * 主函数
     *
     * @param args
     */
    public static void main(String[] args) {
        new ExcelBatchJFrame().start();
    }
}